// packageB/pages/package/liangdeng/bind_positon/bind_positon.js
const mybehavior = require("../../../../../behaviors/requests")
import WxValidate from "../../../../../Wx_Validate/WxValidate"
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/smartg_api/storage_racks_manage",
    company: "",
    manager: "",
    line: "",
    location_code: "",
    position_list: [],
    position_list1: [],
    business_ids: "",
    options: [],
    options1: [],
    options2: [],
    options3: [],
    popup_show1: false,
    popup_show2: false,
    popup_show3: false,
    people: [],
    num: [],

  },

  // 下拉选择
  getPickerVlaue(e) {
    console.log(e);
    let key = e.target.id
    this.setData({
      [key]: e.detail._id,
      cabinet_id_name: e.detail.name,
    })

  },
  // 扫描
  scan1(e) {
    console.log(e.currentTarget.dataset.row_data);
    let key = e.currentTarget.dataset.row_data
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          [key]: label_code
        })
        if (key == "business_ids") {
          self.checkAddNum()
        }
      }, 500)
    }).catch(err => {
      console.log(err);
    });
  },
  checkAddNum() {
    let url = this.data.baseUrl + "/exist_business_id "
    let data = {
      business_id: this.data.business_ids
    }
    this.post(url, data).then(res => {

      console.log("校验受理编号", res);
      if (res.code == 20000) {
        // /
        if (res.data == "受理编号不存在") {
          wx.showToast({
            title: res.data,
            icon: 'error'
          })
          this.setData({
            business_ids: ""

          })

        } else {
          let arr = this.data.position_list
          let arr1 = this.data.position_list1
          console.log(arr.length);
          let obj = {
            index: arr.length + 1,
            business_ids: this.data.business_ids
          }
          arr.push(obj)
          arr1.push(this.data.business_ids)
          this.setData({
            position_list: arr,
            position_list1: arr1,
            business_ids: ""
          })
        }

        // /

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 添加库位
  addPosition() {

    if (this.data.business_ids == "") {
      wx.showToast({
        title: '受理编号为空，无法添加!',
        icon: 'none'
      })
    } else {
      if (this.data.position_list1.includes(this.data.business_ids)) {
        wx.showToast({
          title: '请勿重复添加!',
          icon: 'none'
        })

      } else {
        let url = this.data.baseUrl + "/exist_business_id "
        let data = {
          business_id: this.data.business_ids
        }
        this.post(url, data).then(res => {

          console.log("校验受理编号", res);
          if (res.code == 20000) {
            // /
            if (res.data == "受理编号不存在") {
              wx.showToast({
                title: res.data,
                icon: 'error'
              })
              this.setData({
                business_ids: ""

              })

            } else {
              let arr = this.data.position_list
              let arr1 = this.data.position_list1
              console.log(arr.length);
              let obj = {
                index: arr.length + 1,
                business_ids: this.data.business_ids
              }
              arr.push(obj)
              arr1.push(this.data.business_ids)
              this.setData({
                position_list: arr,
                position_list1: arr1,
                business_ids: ""
              })
            }

            // /

          } else {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
        }).catch(err => {
          console.log(err);
          wx.showToast({
            title: err.msg,
            icon: 'error'
          })
        })




      }
    }
  },
  // 删除库位
  deletePosition(e) {
    console.log(e.currentTarget.dataset.row_data);
    let key = e.currentTarget.dataset.row_data - 1
    let arr = this.data.position_list
    let arr1 = this.data.position_list1
    arr.splice(key, 1)
    for (let i in arr) {
      arr[i].index = Number(i) + Number(1)
    }
    arr1.splice(key, 1)
    this.setData({
      position_list: arr,
      position_list1: arr1,
    })
  },

  // 数据重置
  reset() {
    this.setData({
      cabinet_id_name: "",
      cabinet_id: "",
      position: "",
      location_code: "",
      position_list: [],
      position_list1: [],
      label_mac: "",
      label_list: [],
      indicator_mac: "",
      indicator_list: [],

    })
  },

  // 循环库位号提交
  preSubmit() {
    const arr = this.data.position_list1
    if (arr.length != 0) {
      for (let i in arr) {
        this.submit(arr[i])
      }
    } else {
      wx.showToast({
        title: '数据为空！',
        icon: 'error'
      })
    }
  },
  // 提交绑定
  submit() {
    wx.showLoading({
      title: '处理中...',
    })
    this.setData({
      submit_disabled: true
    })
    let that = this
    let url = this.data.baseUrl + "/deposit"
    let data = {
      // cabinet_id: "cabinet_id_001",
      cabinet_id: this.data.cabinet_id,
      batch_nos: "",
      location_code: this.data.location_code,
      business_ids: this.data.position_list1,
    }
    console.log(data);

    if (!that.WxValidate.checkForm(data)) {
      const error = that.WxValidate.errorList[0]
      wx.hideLoading()
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
      this.setData({
        submit_disabled: false
      })

    } else {
      this.post(url, data).then(res => {

        if (res.code == 20000) {
          this.setData({
            submit_disabled: false
          })
          wx.showToast({
            title: "提交成功",
            icon: 'success'
          })
          wx.hideLoading()
          this.getPositionDatas()

        } else {
          wx.hideLoading()
          this.setData({
            submit_disabled: false
          })
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }).catch(err => {
        wx.hideLoading()
        this.setData({
          submit_disabled: false
        })
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
      this.setData({
        submit_disabled: false
      })
    }

  },
  // 获取该库位数据信息
  getPositionDatas() {
    let url = this.data.baseUrl + "/query_msg_by_location_code"
    let data = {
      location_code: this.data.location_code,
      cabinet_id: this.data.cabinet_id,
      // cabinet_id: "cabinet_id_001",
    }
    this.post(url, data).then(res => {
      console.log("获取该库位数据信息", res.data)
      let num_arr = []
      let people_arr = []
      for (let i in res.data) {
        num_arr.push(res.data[i].business_id)
        people_arr.push(res.data[i].contact)
      }

      if (res.code == 20000) {
        this.setData({
          num: num_arr,
          people: people_arr,
        })
        this.flashLabel()
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 刷新标签
  flashLabel() {
    let url = "https://logisticsengineering.gree.com/order_system/smart_ld"
    let data = {
      location: this.data.location_code,
      ldORmd: 0,
      people: this.data.people,
      num: this.data.num,
      code: "",
      date: this.formatDate(new Date()),
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        let row_data = {
          position_list: this.data.position_list,
          cabinet_id_name: this.data.cabinet_id_name,
          cabinet_id: this.data.cabinet_id,
          location_code: this.data.location_code,

        }
        // this.reset()
        wx.navigateTo({
          url: '/packageB/pages/package/smart_box/send_car/send_car',
          success(res) {
            res.eventChannel.emit('acceptDataFromOpenerPage', row_data)
          }
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })



  },
  // 成功后删除库位
  // 删除库位
  deletePosition1(e) {
    let key = e
    let arr = this.data.position_list
    let arr1 = this.data.position_list1
    for (let i in arr) {
      if (arr[i].position == key) {
        arr.splice(i, 1)

      }
      if (arr.length != 0) {
        arr[i].index = Number(i) + Number(1)

      }
    }
    for (let j in arr1) {
      if (arr1[j].position == key) {
        arr1.splice(i, 1)

      }

    }
    this.setData({
      position_list: arr,
      position_list1: arr1,
    })
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      cabinet_id: {
        required: true,
      },
      location_code: {
        required: true,
      },
      business_ids: {
        required: true,
      },
     
      // position: {
      //   required: true,
      // },
      // label_mac: {
      //   required: true,
      // },
      // indicator_mac: {
      //   required: true,
      // },

    }

    let message = {
      cabinet_id: {
        required: "网点名称-不能为空",
      },
      location_code: {
        required: "库位编码-不能为空",
      },
      business_ids: {
        required: "受理编号-不能为空",
      },
      
      // position: {
      //   required: "库位号-不能为空",
      // },
      // label_mac: {
      //   required: "公司名称-不能为空",
      // },
      // position: {
      //   required: "公司名称-不能为空",
      // },

    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },

  // 查询--网点名称--集合
  getCabinetData() {
    let url = "https://logisticsengineering.gree.com/smartg_api/cabinet_manage/query_cabinet";
    let data = {
      page: 1,
      pagesize: 9999,
    }

    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {

        this.setData({
          options1: res.data.results,
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()
    this.getCabinetData()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})